package cn.doitedu.datayiee_service17.service;

import cn.doitedu.datayiee_service17.dao.ImpalaDao;
import cn.doitedu.datayiee_service17.pojo.EventProperty;
import cn.doitedu.datayiee_service17.pojo.LoudouEvent;
import cn.doitedu.datayiee_service17.pojo.LoudouParams;
import cn.doitedu.datayiee_service17.pojo.LoudouRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ImpalaLoudouAnalyseServiceImpl implements ImpalaLoudouAnalyseService {

    @Autowired
    ImpalaDao dao;

    @Override
    public List<LoudouRes> genSql(LoudouParams params) {


        String part1 = "with tmp as \n" +
                "(\n" +
                "SELECT\n" +
                "guid,\n" +
                "sortarray(group_concat(concat_ws('_',eventid,cast(ts as string)),',')) as events\n" +
                "FROM test.action_dtl_ld_parquet,test.action_dtl_ld_parquet.properties b \n" +
                "WHERE  \n";


        List<LoudouEvent> events = params.getEvents();

        ArrayList<String> evs = new ArrayList<>();
        for (LoudouEvent event : events) {
            String event_id = event.getEvent_id();
            List<EventProperty> properties = event.getProperties();

            // (eventid='e1' and b.key='p1' and b.value='v1')
            String evid = "(eventid='"+event_id+"'";
            StringBuilder sb = new StringBuilder();
            sb.append(evid);
            for (EventProperty property : properties) {
                sb.append(" and b.key='"+property.getName()+"' and b.value"+property.getOper()+"'"+property.getValue()+"'");
            }
            evs.add(sb.toString());
        }

        String where = "";
        for (String ev : evs) {
            where += ev + ") or ";
        }

        String part2 = where.substring(0,where.length()-4);

        String part3 = " GROUP BY guid\n" +
                ")";


        /**
         * ,tmp2 as (
         * SELECT
         *   guid,
         *   if(regexp_extract(events,'.*?(e1).*?',1) ='e1',1,0) as step1,
         *   if(regexp_extract(events,'.*?(e1).*?(e4).*?',2) ='e4',1,0) as step2,
         *   if(regexp_extract(events,'.*?(e1).*?(e4).*?(e6).*?',3) ='e6',1,0) as step3
         * FROM tmp
         * )
         */

        String part4 = ",tmp2 as (\n" +
                "SELECT\n" +
                "  guid,";




        ArrayList<String> ifList = new ArrayList<>();
        String common = "if(regexp_extract(events,'";
        String ele = "";
        for(int i=0;i<events.size();i++){
            ele += ".*?("+events.get(i).getEvent_id()+")";
            ifList.add(ele);
        }

        StringBuilder sb2 = new StringBuilder();
        for (int i=0;i<ifList.size();i++) {
            sb2.append(common+ifList.get(i)+".*?',"+(i+1)+")='"+events.get(i).getEvent_id()+"',1,0) as step"+(i+1)).append(",");
        }
        String part5 = sb2.toString().substring(0,sb2.length()-1);


        String part6_1 = "" +
                "\n FROM tmp\n" +
                ")\n" +
                "\n" +
                "\n" +
                "SELECT\n" +
                "'牛逼轰轰' as ld_name,\n" ;

        StringBuilder ssb = new StringBuilder();
        for(int i=1;i<=evs.size();i++) {
              ssb.append("sum(step"+i+") as step"+i+"_users," );
          }


        String part6_2 = " \n FROM tmp2";

        String part6 = part6_1+ssb.substring(0,ssb.length()-1)+part6_2;


        List<LoudouRes> loudouRes = dao.loudouQuery(part1 + part2 + part3 + part4 + part5 + part6,evs.size());

        return loudouRes;
    }
}
